package model

import (
	"ginProject/biz/dao"
)

type CustomerLogin struct {
	ID           int    `json:"id"`
	LoginName    string `json:"login_name"`
	Password     string `json:"password"`
	IsAdmin      int
	Status       int
	CreateTime   string
	ModifiedTime string
}

/**
 * @Author leibidong
 * @Description 根据账号，密码，查询数据库
 * @Date  2021/7/3 10:53
 **/
func Login(loginName string) (c CustomerLogin) {
	db := dao.InitDB()
	var customerLogin CustomerLogin
	db.Raw("select * from customer_login where login_name=? and is_admin=1", loginName).Scan(&customerLogin)
	return customerLogin
}

/**
 * @Author leibidong
 * @Description 插入新注册的管理员
 * @Date  2021/7/3 11:17
 **/
func InsertLogin(c CustomerLogin) {
	db := dao.InitDB()
	db.Exec("insert into customer_login(login_name,password,is_admin,status,create_time,modified_time) "+
		"values(?,?,?,?,?,?)", c.LoginName, c.Password, c.IsAdmin, c.Status, c.CreateTime, c.ModifiedTime)
}

/**
 * @Author leibidong
 * @Description 修改管理员登录状态
 * @Date  2021/7/3 11:18
 **/
func UpdateStatus(id int) {
	db := dao.InitDB()
	db.Exec("update customer_login set status=1 where id=? and is_admin=1", id)
}

/**
 * @Author leibidong
 * @Description 注销管理员登录状态,将status修改为0
 * @Date  2021/7/3 13:15
 **/
func UpdateStatusToZero(id int) {
	db := dao.InitDB()
	db.Exec("update customer_login set status=0 where id=? and is_admin=1", id)
}

/**
 * @Author leibidong
 * @Description 根据用户管理员的id查看状态
 * @Date  2021/7/3 13:13
 **/
func QueryStatusById(id int) (status int) {
	db := dao.InitDB()
	var customer CustomerLogin
	db.Raw("select status from customer_login where id=? and is_admin=1", id).Scan(&customer)
	if customer.Status == 0 {
		return 0
	} else {
		return 1
	}
}

/**
 * @Author leibidong
 * @Description 根据管理员id查询账号信息
 * @Date  2021/7/3 16:39
 **/
func QueryById(id int) CustomerLogin {
	db := dao.InitDB()
	var customer CustomerLogin
	db.Raw("select * from customer_login where id=? and is_admin=1", id).Scan(&customer)
	return customer
}
